<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>Barpedia, votre compagnon anti-soif.</title>
        <link href="style.css" rel="stylesheet" media="all" type="text/css">       
        <!-- Où s'abreuver dans Paris. --> 
    </head>
    <body>
        <div id="header">
            <h1><a href="index.php"><img src="./media/Logobarpedia.png" height="25%" width="25%"/></a></h1>
            <form method="post" action="index.php">
                <input type="submit" name="create" value="Ajouter un bar" />
            </form>
        </div>
        <div id="searchbar">
            <form method="post" action="index.php">
                <input name="cp" type="text" placeholder="Numéro d'arrondissement (ex: 5)"/>
                <input name="name" type="text" size="40" placeholder="Nom du bar"/>
                <button class="buttonSearch" type="submit" name="Submit" value="Search">Rechercher</button>
            </form>
        </div>
        <div id="content">
            <?php
            include 'include/dbconnect.php';
            if (isset($_POST['enregistrer'])) {
                $name = mysql_real_escape_string(trim($_POST['barname']));
                $adresse = mysql_real_escape_string(trim($_POST['baradresse']));
                $cp = mysql_real_escape_string(trim($_POST['barcp']));
                $description = mysql_real_escape_string(trim($_POST['bardescription']));
                $url = mysql_real_escape_string(trim($_POST['barurl']));
                $note = mysql_real_escape_string(trim($_POST['barnote']));
                if (strlen($description) <= 2000) {
                    if ($name && $adresse && $cp) {
                        $insert_sql = "INSERT INTO bar (`idBar`, `name`, `adresse`, `cp`, `ville`, `description`, `url`, `note`) VALUES(NULL,'$name','$adresse','$cp',NULL,'$description','$url','$note');";
                        mysql_query($insert_sql);
                        echo '<script type="text/javascript"> alert("Bar enregistré! Pour fêter ça, buvons un coup.") </script>';
                        die(include './include/main.php');
                    } else {
                        echo '<script type="text/javascript"> alert("Veuillez remplir les champs obligatoires.") </script>';
                        include 'include/createform.php';
                    }
                } else {
                    echo '<script type="text/javascript"> alert("Votre description est trop longue.") </script>';
                    include 'include/createform.php';
                }
            } else {
                if (!isset($_POST['create'])) {
                    if (isset($_POST['cp']) || isset($_POST['name'])) {
                        include 'include/result.php';
                    } else {
                        include 'include/main.php';
                    }
                } else {
                    include 'include/createform.php';
                }
            }
            ?>
        </div>
        <div id="footer"><p>Projet programmation OHO/ Base de données - Ionis STM</p> 
            <p>LEBLANC Cédric / JOURDAN Pascal / OLLARI Thomas - Bachelor MSI</p></div>
        <div id="footer2"> </div>
</html>